package com.leve.control.controller;

import com.leve.control.constant.Constants;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.Device;
import com.leve.control.service.DeviceService;
import com.leve.control.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author whongyu
 * @create by 2019/7/1
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/device")
@Api(tags = {"pc-设备相关接口服务"})
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "添加设备信息接口")
    @PutMapping(value = "/add")
    @ResponseBody
    public Result addDevice(@RequestBody Device device){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/device/add|添加设备信息接口,device={}: "+ device);
        try{
            int i = deviceService.addDevice(device);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }
            return new Result(ErrorCode.E_20029);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/device/add|添加设备信息接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "编辑设备信息接口")
    @PutMapping(value = "/edit")
    @ResponseBody
    public Result editDevice(@RequestBody Device device){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/device/edit|编辑设备信息接口,device={}: "+ device);
        try{
            int i = deviceService.editDevice(device);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }
            return new Result(ErrorCode.E_20029);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/device/edit|编辑设备信息接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }
}
